﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RaptorEngine.Components;
using Microsoft.Xna.Framework;
using RaptorEngine;
using Microsoft.Xna.Framework.Input;
using OilRigSumoWrestlers.KeyBindings;
using RaptorEngine.Managers;
using OilRigSumoWrestlers.Entities.MenuItems;
using OilRigSumoWrestlers.Levels;
using OilRigSumoWrestlers.Controllables.GameModes;
using OilRigSumoWrestlers.Scenes;
using OilRigSumoWrestlers.Entities;

namespace OilRigSumoWrestlers.Controllables.Controllers
{
    class LocalLobbyController : Controllable
    {
        public LocalLobbyController(Entity entity)
            : base(entity)
        {
        }

        public override void Update(GameTime gameTime)
        {
            if (GameEngine.Instance.Input.IsPressed(Keys.Enter))
            {
                new PlayerKeyboardBindings(LogicalPlayerIndex.Two).ActivateBindings();
                GoForwards();
            }
            else if (GameEngine.Instance.Input.IsPressed(Keys.Escape)
                || GameEngine.Instance.Input.IsPressed(Buttons.B, LogicalPlayerIndex.Any))
            {
                GameEngine.Instance.Scene.PopScene();
                GameEngine.Instance.Scene.ShowCurrentScene();
                GameEngine.Instance.Scene.StartCurrentScene();
            }
            else
            {
                for (PlayerIndex i = PlayerIndex.One; i <= PlayerIndex.Four; i++)
                {
                    if (!GameEngine.Instance.Input.IsPlayerBound(LogicalPlayerIndex.One, i)
                    && (GameEngine.Instance.Input.IsPressed(Buttons.A, i)
                    || GameEngine.Instance.Input.IsPressed(Buttons.Start, i)))
                    {
                        GameEngine.Instance.Input.SetLogicalPlayer(LogicalPlayerIndex.Two, i);
                        SetGamepadBindings();
                    }
                }
            }
        }

        private void SetGamepadBindings()
        {
            new PlayerGamepadBindings(LogicalPlayerIndex.Two).ActivateBindings();
            GoForwards();
        }

        protected void GoForwards()
        {
            GameEngine.Instance.Scene.HideCurrentScene();
            GameEngine.Instance.Scene.StopCurrentScene();

            // Player 1
            PlayerCharacter player1 = new PlayerCharacter("Models/Characters/player1");
            //player1.Position = new Vector3(20, 20, 10);
            player1.AddComponent(new HumanControllable(player1, LogicalPlayerIndex.One));
            player1.AddComponent(new ParticleComp(player1, ComponentType.Particle, "smoke"));

            // Player 2
            PlayerCharacter player2 = new PlayerCharacter("Models/Characters/player2");
            //player2.Position = new Vector3(20, 20, 30);
            player2.AddComponent(new HumanControllable(player2, LogicalPlayerIndex.Two));
            player2.AddComponent(new ParticleComp(player2, ComponentType.Particle, "smoke"));

            // Level design
            ALevel level = new LevelTest();
            player1.Position = level.GetPlayer1StartPosition();
            player2.Position = level.GetPlayer2StartPosition();

            // Game Mode
            Entity gamemode = new Entity();
            KnockoutMode ko = new KnockoutMode(gamemode, player1, player2, -20f, 3, level);
            gamemode.AddComponent(ko);

            GameEngine.Instance.Scene.PushScene(new LevelScene(player1, player2, level, gamemode, false));
            GameEngine.Instance.Scene.ShowCurrentScene();
            GameEngine.Instance.Scene.PushScene(new CountdownScene());
            GameEngine.Instance.Scene.StartCurrentScene();
            GameEngine.Instance.Scene.ShowCurrentScene();
        }
    }
}
